<?php
namespace ChannelThrift\Service;

use Base\BaseService;
use \ChannelThrift\Service\AntNestService;

class CarDealerService extends BaseService {
    Protected $autoCheckFields = false;
    public  $connection = 'DB_AUMS';
    public $tablePrefix = 'au_';


    /**
     * 获取单个车商信息
     * @param integer $dealerId
     * @return array
     */
    public function getCarDealer($dealerId)
    {        
        if (empty($dealerId) ) {
            return false;
        }
        return D('ChannelThrift/Aums/CarDealer')->where("dealer_id = '" . $dealerId."'")->find();
    }

    /**获取多个车商信息
     * @param string $dealerIds
     * @return bool|mixed
     */
    public function getCarDealers($dealerIds){
        if(empty($dealerIds)){
            return false;
        }        
        return D('ChannelThrift/Aums/CarDealer')->where("dealer_id in (" . $dealerIds.")")->select();
    }

    /**
     * 更新车商保证金信息
     * @param integer $dealerId
     * @param double $bailAmount
     * @param double $freezeAmount
     * @return bool
     */
    public function updateCarDealerBail($dealerId, $data)
    {
        if (empty($dealerId) ) {
            return false;
        }

        return D('ChannelThrift/Aums/CarDealer')->where('dealer_id = ' . $dealerId)->save( $data );
    }

    /**
     * 更新车商基本信息
     * @param $dealerId 车商ID
     * @param $data 更新数据
     * @author Eric
     * @date 2016-11-22
     * @return mix
     */
    public function updateCarDealer($dealerId,$data){
        if (empty($dealerId) ) {
            return false;
        }
        return D('ChannelThrift/Aums/CarDealer')->where("dealer_id = '" . $dealerId."'")->save($data);
    }

    /**
     * 车商基本详细
     * @return   Object
     */
    public function dealerBaseInfo($dealer_id)
    {
        $map = array('d.dealer_id' => array('eq', $dealer_id));
        $fields = 'd.*,du.uid as uid,e.emp_id as manager_id,e.real_name manager_name';
        $prefix = C('DB_AUMS.DB_PREFIX');
        $row = D('ChannelThrift/Aums/CarDealer')->alias('d')->field($fields)
            ->join("LEFT JOIN {$prefix}dealer_user as du ON du.dealer_id=d.dealer_id")
            ->join("LEFT JOIN {$prefix}dealer_manager as dm ON d.dealer_id=dm.dealer_id")
            ->join("LEFT JOIN {$prefix}employee e ON dm.emp_id=e.emp_id")
            ->where($map)->find();
        if($row){
            $result = $this->getUserInfo($row['uid']);
            return array_merge($row,$result);
        }
        return $row;
    }

    /**
     * 根据uid获取用户信息
     * @param string $uid 用户id
     * @return array
     */
    public  function  getUserInfo($uid = ''){
        $antnest = new AntNestService(C('APP_KEY'),C('SECRET_KEY'));
        $option  = json_encode(array('account_type'=>C('ACCOUNT_TYPE'),'account_id'=>$uid));
        $info = $antnest->juhecurl(C('ANT_URL').C('USE_ROUTE.get_info_by_uid'),$option);
        $info = json_decode($info,true);
        if($info['code'] === "000000"){
            $result = array(
                'user_name'=>$info['data']['account_name'],
                'uid'=>$info['data']['account_id'],
                'pwd_password'=>$info['data']['pwd'],
                'pwd_mobile'=>$info['data']['mobile'],
                'nick_name'=>$info['data']['nick_name'],
                'real_name'=>$info['data']['real_name'],
                'avatar'=>$info['data']['account_avatar'],
                'status'=>$info['data']['status'],
                'createtime'=>$info['data']['reg_date'],
                'las_login_time'=>$info['data']['las_login_time'],
                'updatetime'=>$info['data']['last_modify_time'],
            );
            return $result;
        }else{
            trace("蚁巢返回错误：".var_export($info,true),'','ERR',true);
        }
        return false;
    }

    public function getJpushId($uid){
        if (empty($uid) ) {
            return false;
        }
        $sql = "SELECT dev_jpush_id  FROM au_app_devices WHERE dev_uid='{$uid}' order by dev_modifytime DESC limit 1";
        $res = $this->query($sql);
        return $res[0]['dev_jpush_id'];
    }

    /**
     * 更新车商出价的车信息
     * @param $dealer_id
     * @param $order_id
     * @param $last_price
     * @param string $bid_type
     */
    public function dealerBidPrice($dealer_id,$order_id,$last_price,$bid_type='bid'){
            $time = ms_time();
            $m = D('ChannelThrift/Aums/DealerBid');
            //检查是否已有出价记录
            $map = array();
            $map['dealer_id'] = $dealer_id;
            $map['order_id'] = $order_id;
            $check_rs = $m->where($map)->find();
            if(empty($check_rs)){
                $data = array();
                $data['dealer_id']=$dealer_id;
                $data['order_id'] = $order_id;
                if($bid_type=='bid'){
                    $data['last_bid_price']=$last_price;
                }elseif($bid_type=='bidding'){
                    $data['last_bidding_price']=$last_price;
                }
                $data['updatetime'] = $time;
                $data['createtime'] = $time;
                D('ChannelThrift/Aums/DealerBid')->add($data);
            }else{
                $data = array();
                if($bid_type=='bid'){
                    $data['last_bid_price']=$last_price;
                }elseif($bid_type=='bidding'){
                    $data['last_bidding_price']=$last_price;
                }
                $data['updatetime'] = $time;
                $m->where("id = '" . $check_rs['id']."'")->save($data);
            }
    }
}
